//
//  QCAutoCheckCell.swift
//  爱汽车
//
//  Created by tucici on 2024/3/20.
//

import UIKit

class QCAutoCheckCell: UICollectionViewCell {
    @IBOutlet var title: UILabel!
    @IBOutlet var bgView: UIView!
    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet var status: UILabel!
    @IBOutlet var img: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
        initUI()
    }
    func initUI(){
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
    }
    var model = QCCheckMode.auto("", 0, [""]){
        didSet{
            if case let .auto(text,statusCode,error) = model{
                title.text = text
                collectionView.reloadData()
                switch statusCode{
                case 0:///失败
                    img.layer.removeAllAnimations()
                    img.image = UIImage(named: "检测失败")
                    status.text = LS("检测失败")
                    status.textHexColor = "#FFFFFF"
                case 1:///成功
                    img.layer.removeAllAnimations()
                    img.image = UIImage(named: "检测成功")
                    status.text = LS("检测成功")
                    status.textHexColor = "#FFFFFF"
                case 2:///进行中
                    img.image = UIImage(named: "检测中")
                    status.text = LS("检测中...")
                    status.textHexColor = "#747474"
                    addWaveAnimation()
                default:
                    break
                }
            }
        }
    }
    private func addWaveAnimation() {
        //        if statusImg.layer.animationKeys()?.count ?? 0 > 0 {return}
        img.layer.removeAllAnimations()
        let waveAnimation = CABasicAnimation(keyPath: "transform")
        waveAnimation.duration = 1.0
        waveAnimation.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
        waveAnimation.toValue = NSValue(caTransform3D: CATransform3DMakeScale(0.7, 0.8, 1.0))
        waveAnimation.autoreverses = true
        waveAnimation.repeatCount = .infinity
        img.layer.add(waveAnimation, forKey: "waveDeformationAnimation")
    }
}

//MARK: - UICollectionViewDataSource
extension QCAutoCheckCell:UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if case let .auto(_,_,error) = model{
            return error.count
        }
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"cell", for:indexPath)
        cell.contentView.removeAllSubView()
        cell.contentView.backgroundColor = .clear
        let lbl = UILabel()
        lbl.font = UIFont(name: "Lato-Regular", size: 18)!
        lbl.textHexColor = "#FF4D00"
        lbl.textAlignment = .center
        cell.contentView.addSubview(lbl)
        lbl.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.top.bottom.equalToSuperview()
        }
        if case let .auto(_,_,error) = model{
            lbl.text = LS(error[indexPath.row])
        }
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension QCAutoCheckCell:UICollectionViewDelegate
{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        
    }
}
//MARK: - UICollectionViewDelegateFlowLayout
extension QCAutoCheckCell:UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        return  CGSize(width:collectionView.bounds.size.width , height: 30)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return .zero
    }
}
